<?php
/*$post = $_POST;
// 过滤一下
foreach ($post as &$p) {
	$p = htmlspecialchars(strip_tags($p));
}

// 测试返回一个文章列表
if ($post['srcLangCode'] && $post['srcLangCode'] && $post['langDir']) {
	$fileList = array(
		'en-GB.com_content1.ini',
		'en-GB.com_content2.ini',
		'en-GB.com_content3.ini',
	);
	echo json_encode($fileList);
}

if ($post['filename']) {
	echo "success";
}

die;
*/
/////////////////////////////////
// 载入库文件
require_once("./lib/IniParser.php");

$post = $_POST;
// 过滤一下
foreach ($post as &$p) {
	$p = htmlspecialchars(strip_tags($p));
}

$patternInitCode = "/^[a-z]{2}-[A-Z]{2}$|^[a-z]{2}$/";

session_start();
if (			// 翻译文件
	isset($_SESSION['JoomlaIniTranslator']) && 
	$_SESSION['JoomlaIniTranslator'] instanceof IniParser &&
	isset($post['filename'])
) {
	$ip = $_SESSION['JoomlaIniTranslator'];
	if ($ip->translateNextFile() === true) {
		echo "success";
	}
} else if (		// 初始化
	preg_match($patternInitCode, $post['srcLangCode']) == 1 &&
	preg_match($patternInitCode, $post['tarLangCode']) == 1 &&
	isset($post['langDir'])
) {
	unset($_SESSION['JoomlaIniTranslator']);
	$sourceLanguageCode = $post['srcLangCode'];
	$sourceLanguageDir = $post['srcLangDir'];
	$targetLanguageCode = $post['tarLangCode'];
	$targetLanguageDir = $post['tarLangDir'];
	$languageDirectory = $post['langDir'];
	$ip = new IniParser($sourceLanguageCode, $sourceLanguageDir, $targetLanguageCode, $targetLanguageDir, $languageDirectory);
	$fileList = $ip->init();
	echo json_encode($fileList);
}
?>